//
//  PhotoBrowerCell.swift
//  weibopush
//
//  Created by 龚欣 on 15/7/3.
//  Copyright © 2015年 gong. All rights reserved.
//

import UIKit
import SDWebImage
import FFAutoLayout

protocol photoBrowerCellDelegate : NSObjectProtocol{
    func photoBrowerCellIconDidTap()
    func photoBrowerDidZoom(scale : CGFloat)
    func photoBrowerDidEndZoom()
}

class PhotoBrowerCell: UICollectionViewCell,UIScrollViewDelegate {

    var imageURL : NSURL?{
        didSet{
            
            resetScrollView()
            indicator.startAnimating()
            iconView.sd_setImageWithURL(imageURL!, placeholderImage: nil) { (image, error, _, _) -> Void in
                if error == nil{
                    self.indicator.stopAnimating()
                    self.imagePosition(image)
                }
            }
        }
    }
    
    weak var photoCellDelegate : photoBrowerCellDelegate?
    
    // MARK: 让整个图片宽度为整个屏幕，然后通过图片原有的长宽比例缩放图片
    func imagePosition(image : UIImage){
        let size = displaySize(image)
        
        if size.height > bounds.height{
        //长微博
            iconView.frame = CGRect(origin: CGPointZero, size: size)
            scrollView.contentSize = size
        }else{
        //短微博
            let y = (bounds.height - size.height) * 0.5
            iconView.frame = CGRect(origin: CGPointZero, size: size)
            scrollView.contentInset = UIEdgeInsets(top: y, left: 0, bottom: 0, right: 0)
        }
        
    }
    
    func displaySize(image : UIImage) -> CGSize{
        let scale = image.size.height / image.size.width
        let h = UIScreen.mainScreen().bounds.size.width * scale
        return CGSizeMake(UIScreen.mainScreen().bounds.size.width, h)
    }
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(scrollView)
        scrollView.addSubview(iconView)
        addSubview(indicator)
        
        iconView.userInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: "tapClick")
        iconView.addGestureRecognizer(tap)
        
        scrollView.backgroundColor = UIColor.clearColor()
        scrollView.delegate = self
        scrollView.frame = UIScreen.mainScreen().bounds
        scrollView.minimumZoomScale = 0.5
        scrollView.maximumZoomScale = 2
        
        indicator.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil)
    }
    
    func tapClick(){
        photoCellDelegate!.photoBrowerCellIconDidTap()
    }
    
    // MARK: scrollView代理方法
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return iconView
    }
    
    func resetScrollView(){
        scrollView.contentInset = UIEdgeInsetsZero
        scrollView.contentOffset = CGPointZero
        scrollView.contentSize = CGSizeZero
        iconView.transform = CGAffineTransformIdentity
    }
    
    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {

       var offsetX = (scrollView.bounds.size.width - view!.frame.size.width) * 0.5

        if offsetX < 0 {
            offsetX = 0
        }
        
        var offsetY = (scrollView.bounds.size.height - view!.frame.size.height) * 0.5
        if offsetY < 0 {
            offsetY = 0
        }
        
        scrollView.contentInset = UIEdgeInsets(top: offsetY, left: offsetX, bottom: 0, right: 0)
        photoCellDelegate?.photoBrowerDidEndZoom()
    }
    
    func scrollViewDidZoom(scrollView: UIScrollView) {
        let scale = iconView.transform.a
        photoCellDelegate?.photoBrowerDidZoom(scale)
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var scrollView = UIScrollView()
    lazy var iconView = UIImageView()
    lazy var indicator : UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
    
}
